<?php
/*
* WATERMARK
* Version:		1.0
* Date:			14Jan08
* Author: 		ThanhNV
* 
* Descrition:
*	- Input: 
*		+ image source: allow image types .gif, .png, .jpg, .jpeg
*		+ image watermark: allow image types that support transparent (.gif, .png)
*		+ text
*	- Output:
*		+ watermarked image
*
*
*/
class watermark{
	var $ArrayAllowImageType = array('jpg','jpeg','gif','png');
	var $ArrayAllowWatermarkType = array('gif','png');
	var $imageSource = '';
	var $imageWatermark = '';
	var $pct = 65;/*percent transparent for merge 2 image: 0-100*/
	var $imageSourceExt = 'jpg';
	var $imageWatermarkExt = 'gif';
	var $imageWatermarked = '';
	/**/
	var $fontFamily = 'arial.ttf';
	var $angle = 0;
	var $text = '';
	var $colorR = '00';
	var $colorG = '00';
	var $colorB = '00';
	
	var $shadowR = 'FF';
	var $shadowG = 'FF';
	var $shadowB = 'FF';
	
	function watermark(){
		/*$pct = intval($pct);
		if($pct > 0 && $pct < 100){
			$this->pct = $pct;
		}*/
	}
	/*function setFontFamily($fontFamily){
		$this->fontFamily = $fontFamily;
	}
	function setFontColor($R, $G, $B){
		$this->fontColor = imagecolorallocate($this->imageWatermarked, $R, $G, $B);
	}*/
	function getFileExtension($file){
		return strtolower(substr($file,strrpos($file,'.')+1));
	}
	function setImageSource($imageSource){
		if(is_file($imageSource)){
			$ext = $this->getFileExtension($imageSource);
			if(in_array($ext, $this->ArrayAllowImageType)){
				$this->imageSourceExt = $ext;
				$this->imageSource = $imageSource;
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
	function setImageWatermark($imageWatermark){
		if(is_file($imageWatermark)){
			$ext = $this->getFileExtension($imageWatermark);
			if(in_array($ext, $this->ArrayAllowWatermarkType)){
				$this->imageWatermarkExt = $ext;
				$this->imageWatermark = $imageWatermark;
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
	function createWatermarkedImage($pct,$fileSave){
		/*pct: percent transparent for merge 2 image: 0-100*/
		$pct = intval($pct);
		if($pct < 0 || $pct > 100){
			$this->pct = 65;
		}
		
		switch($this->imageSourceExt){
			case 'gif': $source= imagecreatefromgif($this->imageSource); break;
			case 'png': $source= imagecreatefrompng($this->imageSource); break;
			case 'jpg': $source= imagecreatefromjpeg($this->imageSource); break;
			case 'jpeg': $source= imagecreatefromjpeg($this->imageSource); break;
		}
		switch($this->imageWatermarkExt){
			case 'gif': $watermark= imagecreatefromgif($this->imageWatermark); break;
			case 'png': $watermark= imagecreatefrompng($this->imageWatermark); break;
			case 'jpg': $watermark= imagecreatefromjpeg($this->imageWatermark); break;
			case 'jpeg': $watermark= imagecreatefromjpeg($this->imageWatermark); break;
		}
		list($imageSourceWidth, $imageSourceHeight) = getimagesize($this->imageSource);
		list($imageWatermarkWidth, $imageWatermarkHeight) = getimagesize($this->imageWatermark);
		/*$imageSourceHeight = imagesy($this->imageSource);  
		$imageWatermarkWidth =  imagesx($this->imageWatermark);
		$imageWatermarkHeight =  imagesy($this->imageWatermark);*/
		
		/*calculate width, height for watermaked image*/
		/*phu thuoc vao muc dich tao watermark ma lay do rong, va do cao khac nhau*/
		/*
		$imgratio=$sourcewidth/$sourceheight;
		if ($imgratio>1){
		$newwidth = $thumbsize;
		$newheight = $thumbsize/$imgratio;
		}
		else{
		$newheight = $thumbsize;
		$newwidth = $thumbsize*$imgratio;
		}
		*/
		/*bool imagecopyresampled  ( resource $dst_image  , resource $src_image  , int $dst_x  , int $dst_y  , int $src_x  , int $src_y  , int $dst_w  , int $dst_h  , int $src_w  , int $src_h  )*/
		$newWidth = $imageWatermarkWidth;
		$newHeight = $imageWatermarkHeight;
		
		$imageSourceScale = $imageSourceWidth/$imageSourceHeight;
		$newScale = $newWidth/$newHeight;
		
		
		/*CALCULATE CROP AND RESIZE IMAGE SOURCE*/
		$scaleW = $newWidth / $imageSourceWidth;
		$scaleH = $newHeight / $imageSourceHeight;
		
		if($scaleW < $scaleH){
			$dst_w = $newWidth;
			$src_w = $imageSourceWidth;
			$src_x = 0;
			$dst_x = 0;
			if($imageSourceScale > $newScale){
				$src_h = $imageSourceHeight;
				$src_y = 0;
				$dst_h = $src_h * $scaleW ;
				$dst_y = floor( ((($src_w / $newScale) - $src_h)/2) * $scaleW );
			}else{
				$src_h = $src_w / $newScale;
				$src_y = floor(($imageSourceHeight - $src_h)/2);
				$dst_y = 0;
				$dst_h = $newHeight;
			}
		}else{
			$dst_h = $newHeight;
			$src_h = $imageSourceHeight;
			$src_y = 0;
			$dst_y = 0;
			if($imageSourceScale < $newScale){
				$src_w = $imageSourceWidth;
				
				$src_x = floor(($imageSourceWidth - $src_w)/2);
				$dst_x = 0;
				$dst_w = $newWidth;
			}else{
				$src_w = $src_h / $newScale;
				
				$src_x = 0;
				$dst_w = $src_w * $scaleH ;
				$dst_x = floor( ((($src_h / $newScale) - $src_w)/2) * $scaleH );
			}
		}
		/*CALCULATE CROP AND RESIZE IMAGE SOURCE*/
		
		//echo "dst_x=".$dst_x."-dst_y=". $dst_y."-src_x=". $src_x."-src_y=". $src_y."-dst_w=". $dst_w."-dst_h=". $dst_h."-src_w=". $src_w."-src_h=". $src_h;
		$this->imageWatermarked = imagecreatetruecolor($newWidth,$newHeight); 
		imagecopyresampled($this->imageWatermarked, $source, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); 
		imagecopymerge($this->imageWatermarked,$watermark,0,0,0,0,$imageWatermarkWidth,$imageWatermarkHeight,$pct);
		
		/*write text shadow*/
		//$textOffsetX = $newWidth/2;
		$textOffsetX = ($newWidth - strlen($this->text) * $this->fontSize * 2/3)/2;
		
		$textOffsetY = ($newHeight-$this->fontSize)-20;
		$shadow = 1;
		
		
		$shadowColor = imagecolorallocate($this->imageWatermarked, $this->shadowR, $this->shadowG, $this->shadowB);
		imagettftext($this->imageWatermarked, $this->fontSize, $this->angle, $textOffsetX+$shadow, $textOffsetY+$shadow, $shadowColor, $this->fontFamily, $this->text);
		
		/*write text*/
		$color = imagecolorallocate($this->imageWatermarked, $this->colorR, $this->colorG, $this->colorB);
		imagettftext($this->imageWatermarked, $this->fontSize, $this->angle, $textOffsetX, $textOffsetY, $color, $this->fontFamily, $this->text);
		/*return imagejpeg($thumb,$thumbpart);//save to file  */
		//return $this->imageWatermarked;
		imagejpeg($this->imageWatermarked,$fileSave);
		
		/*Clear Memory*/
		@imagedestroy( $watermark );
		@imagedestroy( $source );
		@imagedestroy( $thumb );
	}
	function setText($text,$fontSize, $angle, $fontFamily, $R, $G, $B, $shadowR, $shadowG, $shadowB){
		$this->text = $text;
		$this->fontSize = $fontSize;
		$this->angle = $angle;
		$this->fontFamily = $fontFamily;
		$this->colorR = $R;
		$this->colorG = $G;
		$this->colorB = $B;
		$this->shadowR = $shadowR;
		$this->shadowG = $shadowG;
		$this->shadowB = $shadowB;
	}
	
}

?>